package com.quanshu.exchange.ui.gui.profile;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.jungly.gridpasswordview.GridPasswordView;
import com.quanshu.change.login.view.ResetPasswordSuccessfulActivity;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.LoginInterface;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.CountDownTimerUtils;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.shop.base.tool.BaseTool;
import com.quanshu.shop.base.view.BaseTitleBar;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 设置支付密码
 * Created by xiniu003 on 2017/12/14.
 */

public class SetPayPasswordActivity extends BaseActivity {
    @BindView(R.id.enter_pay_password)
    GridPasswordView enterPayPassword;
    @BindView(R.id.input_auch_code)
    EditText inputAuchCode;
    @BindView(R.id.send_auch_code)
    TextView sendAuchCode;
    @BindView(R.id.enter_set)
    Button enterSet;
    @BindView(R.id.set_pay_pwd)
    BaseTitleBar setPayPwd;
    @BindView(R.id.enter_set1)
    TextView enterSet1;

    private String enterPayPasswordStr;
    private String inputAuchCodeStr;
    private String mobileNum;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_pay_password);
        ButterKnife.bind(this);
        mobileNum = currentUser.getMobile();
        if (currentUser.getMobile().startsWith("86")) {
            mobileNum = currentUser.getMobile().substring(2);
        }
        initIntent();
        initView();
        initListener();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    private void initIntent() {

    }

    private void initView() {
        ImmersionBar.with(this).titleBar(setPayPwd).statusBarColor(R.color.white).statusBarDarkFont(true).init();
    }

    private void initListener() {

        setPayPwd.setLeftImage(R.drawable.icon_back_black_2_0).setCenterText("设置支付密码").setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });

        enterPayPassword.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public void onTextChanged(String psw) {

            }

            @Override
            public void onInputFinish(String psw) {
                enterPayPasswordStr = psw;
            }
        });

        RxViewHelper.getInstance().afterTextChangeEvents(inputAuchCode,
                textViewAfterTextChangeEvent -> {
                    inputAuchCodeStr = textViewAfterTextChangeEvent.editable().toString();
                });

        RxViewHelper.getInstance().clicks(sendAuchCode, aVoid -> doSendSms());
        RxViewHelper.getInstance().clicks(enterSet1, aVoid -> setTransPasswd());

    }

    private void doSendSms() {

        NetClient.getInstance().getShopService(LoginInterface.class)
                .doSendSms(AppUtils.getSignParams(mobileNum),
                        mobileNum)
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        ToastHelper.toast(response.getMsg());
                        CountDownTimerUtils timerUtils = new CountDownTimerUtils(sendAuchCode, 60000, 1000);
                        timerUtils.start();
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        super.onFailure(e);
                    }
                });
    }

    //设置支付密码
    private void setTransPasswd() {
        if (!TextUtils.isEmpty(inputAuchCodeStr)) {
            NetClient.getInstance().getShopService(LoginInterface.class)
                    .setTransPasswd(AppUtils.getSignParams(currentUser.getMemberid(),
                            enterPayPasswordStr, enterPayPasswordStr, inputAuchCodeStr, mobileNum),
                            currentUser.getMemberid(),
                            enterPayPasswordStr,
                            enterPayPasswordStr,
                            inputAuchCodeStr,
                            mobileNum)
                    .compose(new BaseTransformer<>(this, true, false))
                    .subscribe(new BaseObserver<BaseResponse>() {
                        @Override
                        public void onSuccess(BaseResponse response) {
                            currentUser.setMy_isPwd(1);
                            Bundle bundle = new Bundle();
                            bundle.putInt(ResetPasswordSuccessfulActivity.TIPS_SHOW, 0);
                            BaseTool.goActivity(SetPayPasswordActivity.this, ResetPasswordSuccessfulActivity.class, bundle);
                            finish();
                        }

                        @Override
                        public void onFailure(Throwable e) {
                            super.onFailure(e);
                        }
                    });
        }
    }

}
